package com.nwpu.coursesupport.view.controller;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.controller.AbstractController;
import com.nwpu.coursesupport.criteria.SCCViewSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.LessonType;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.service.imple.SCCViewService;
import com.nwpu.coursesupport.tool.StringUtil;

@Controller
@RequestMapping("scccontrol")
public class SCCViewController extends AbstractController{
	@Autowired
	private SCCViewService sccViewService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String listView(Integer id,Integer pageNo,HttpServletRequest request) throws ParseException{
		JSONResult rst=null;
		SCCViewSearchCriteria sc=new SCCViewSearchCriteria();
		if(id==null){
			RoleType roleType=(RoleType) getSession().getAttribute("roleType");
			if(roleType!=RoleType.STUDENT){
				rst=new JSONResult();
				rst.fail("无权操作");
				request.setAttribute("data", this.parseData(rst));
				return "/stupage/courseView";
			}
			id=(int) getSession().getAttribute("id");
			
		}
		sc.setId(id);
		sc.setPageNo(pageNo);
		rst=sccViewService.list(sc);
		request.setAttribute("data", this.parseData(rst));
		return "/stupage/courseView";//自己定义
	}
	
	@RequestMapping(value="/ajaxList",method=RequestMethod.GET)
	@ResponseBody
	public JSONResult ajaxListView(String data){
		JSONResult rst=invokeService("sccView", "list",data);
		return rst;
	}
	//TODO:Inform简易版，之后需要删除
	@RequestMapping(value="/listInform",method=RequestMethod.GET)
	@ResponseBody
	public JSONResult listInform(String data){
		JSONResult rst=invokeService("sccView", "listInform",data);
		return rst;
	}
	
	@RequestMapping(value="/ajaxDetailView",method=RequestMethod.GET)
	@ResponseBody
	public JSONResult ajaxDetailView(@RequestParam("data")String data){
		JSONResult rst=invokeService("sccView", "detailView",data);
		return rst;
	}
	
	@RequestMapping(value="/detailView",method=RequestMethod.GET)
	public String detailView(@RequestParam("classId")int id,Integer pageNo,String lessonType,HttpServletRequest request){
		SCCViewSearchCriteria sc=new SCCViewSearchCriteria();
		sc.setId(id);
		sc.setPageNo(pageNo==null?1:pageNo);
		if(StringUtil.isEmpty(lessonType)){
			lessonType=LessonType.THEORY.name();
		}
		if(lessonType.equals(LessonType.LAB.name())){
			sc.setLessonType(LessonType.LAB);
		}else{
			sc.setLessonType(LessonType.THEORY);
		}
		JSONResult rst=this.sccViewService.detailView(sc);
		int courseId =(int) rst.getData().get("courseId");
		request.setAttribute("data", this.parseData(rst));
		request.setAttribute("courseId", courseId);
		return "/stupage/courseLesson";
	}
	

}
